#include <REGX52.h>
#include "Delay.h"

//存储键码
unsigned char KeyNumber = 0;

/**
  * @brief  获取按键键码
  * @param  无
  * @retval 按下按键的键码，范围：0,1~4,0表示无按键按下
  */
unsigned char Key()
{
	unsigned char Temp;
	Temp = KeyNumber;
	KeyNumber = 0;//SoleKey_Looper没有设置将KeyNum的清零的情况，此处清零
	return Temp;
}

/**
  * @brief  此时按键按下情况
  * @parm	无
  * @retval	独立按键的键码
  */
unsigned char SoleKey()
{
	unsigned char keyNum = 0;
	if(P3_1 == 0){keyNum = 1;}
	if(P3_0 == 0){keyNum = 2;}
	if(P3_2 == 0){keyNum = 3;}
	if(P3_3 == 0){keyNum = 4;}
	return keyNum;
}


/**
  * @brief		用定时器中断每20ms调用该函数，检测按键按下；按键驱动函数，在定时器中断中调用
  * @parm		无
  * @retval		被按下的键码值
  */
void SoleKey_Looper()
{
	//记录本次按键按下和上次按键按下
	static unsigned char CurKey = 0, LastKey = 0;
	LastKey = CurKey;
	CurKey = SoleKey();
	if(CurKey == 0 && LastKey == 1)			KeyNumber = 1;
	else if(CurKey == 0 && LastKey == 2)	KeyNumber = 2;
	else if(CurKey == 0 && LastKey == 3)	KeyNumber = 3;
	else if(CurKey == 0 && LastKey == 4)	KeyNumber = 4;
}